<?php

namespace Admin\Controller;

class MemberController extends ComController
{
    public function index()
    {
        
        $p = I('get.p',1);
        $user = I('get.user','');
        $name = I('get.name','');
        $phone =I('get.phone','');
        $order = I('get.order','DESC');
        $where = '';
        $prefix = C('DB_PREFIX');
        if ($order == 'asc') {
            $order = "{$prefix}member.t asc";
        } elseif (($order == 'desc')) {
            $order = "{$prefix}member.t desc";
        } else {
            $order = "{$prefix}member.uid asc";
        }
            if ($user) {
                $where = "{$prefix}member.user LIKE '%$user%'";
            }
            if ($name) {
                $where = "{$prefix}member.name LIKE '%$name%'";
            }
            if ($phone) {
                $where = "{$prefix}member.phone LIKE '%$phone%'";
            }
        $user = M('member');
        $pagesize = 5;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $user->field("{$prefix}member.*,{$prefix}auth_group.id as gid,{$prefix}auth_group.title")
            ->order($order)
            ->join("{$prefix}auth_group_access ON {$prefix}member.uid = {$prefix}auth_group_access.uid")
            ->join("{$prefix}auth_group ON {$prefix}auth_group.id = {$prefix}auth_group_access.group_id")
            ->where($where)
            ->count();
        // dump($count);die;

        $list = $user->field("{$prefix}member.*,{$prefix}auth_group.id as gid,{$prefix}auth_group.title")
            ->order($order)
            ->join("{$prefix}auth_group_access ON {$prefix}member.uid = {$prefix}auth_group_access.uid")
            ->join("{$prefix}auth_group ON {$prefix}auth_group.id = {$prefix}auth_group_access.group_id")
            ->where($where)
            ->limit($offset . ',' . $pagesize)
            ->select();
        //$user->getLastSql();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $group = M('auth_group')->field('id,title')->select();
        $this->assign('group', $group);
        $this->display();
    }

    public function del()
    {
        $uids = isset($_REQUEST['uids']) ? $_REQUEST['uids'] : false;
        //uid为1的禁止删除
        if ($uids == 1 or !$uids) {
            $this->error('参数错误！');
        }
        if (is_array($uids)) {
            foreach ($uids as $k => $v) {
                if ($v == 1) {//uid为1的禁止删除
                    unset($uids[$k]);
                }
                $uids[$k] = intval($v);
            }
            if (!$uids) {
                $this->error('参数错误！');
                $uids = implode(',', $uids);
            }
        }

        $map['uid'] = array('in', $uids);
        if (M('member')->where($map)->delete()) {
            M('auth_group_access')->where($map)->delete();
            addlog('删除会员UID：' . $uids);
            $this->success('恭喜，用户删除成功！');
        } else {
            $this->error('参数错误！');
        }
    }

    public function edit()
    {

        $uid = isset($_GET['uid']) ? intval($_GET['uid']) : false;
        if ($uid) {
            //$member = M('member')->where("uid='$uid'")->find();
            $prefix = C('DB_PREFIX');
            $user = M('member');
            $member = $user->field("{$prefix}member.*,{$prefix}auth_group_access.group_id")->join("{$prefix}auth_group_access ON {$prefix}member.uid = {$prefix}auth_group_access.uid")->where("{$prefix}member.uid=$uid")->find();

        } else {
            $this->error('参数错误！');
        }
        // $where['id']=array('in',array($member['prov'],$member['city'],$member['dist']));
        // $data=M('city')->where($where)->field('id,name')->select();
        // foreach($data as $k=>$v){
        //     $arr[$v['id']]=$v['name'];            
        // }
        // $member['prov']=$arr[$member['prov']];
        // $member['city']=$arr[$member['city']];
        // $member['dist']=$arr[$member['dist']];
        $usergroup = M('auth_group')->field('id,title')->select();
        $this->assign('usergroup', $usergroup);
        $this->assign('member', $member);
        $this->display('form');
    }

    public function update()
    {   
        $data['uid'] = isset($_POST['uid']) ? intval($_POST['uid']) : false;
        $group_id = isset($_POST['group_id']) ? intval($_POST['group_id']) : 0;
        if (!$group_id) {
            $this->error('请选择用户组！');
        }
        $password = isset($_POST['password']) ? trim($_POST['password']) : false;
        if ($password) {
            $data['password'] = password($password);
        }
        $head = I('post.head', '', 'strip_tags');
        $data['head'] = $head ? $head : '';
        $data['group_id']=$group_id ? $group_id:'';
        $data['phone'] = isset($_POST['phone']) ? trim($_POST['phone']) : '';
        $data['user'] = isset($_POST['user']) ? htmlspecialchars($_POST['user']) : '';
        $data['prov'] = I('post.prov', '', 'strip_tags');
        $data['city'] = I('post.city', '', 'strip_tags');
        $data['dist'] = I('post.dist', '', 'strip_tags');
        $data['com'] = I('post.com', '', 'strip_tags');
        $data['address'] = I('post.address', '', 'strip_tags');
        $data['content'] = I('post.content');
        $data['lng'] = I('post.lng', '', 'strip_tags');
        $data['lat'] = I('post.lat', '', 'strip_tags');
        $data['name'] = I('post.name', '', 'strip_tags');
            if (empty($data['user'])) {
                $this->error('用户名称不能为空！');
            }
            if (empty($data['password'])) {
                $this->error('用户密码不能为空！');
            }     
           
            if(empty($data['prov'])){
                 $this->error('请选择省份');
            }
            if(empty($data['city'])){
                $this->error('请选择城市');
            }
            if(empty($data['com'])){
                 $this->error('请填写小区名称');
            }

        if($data['uid']==0){
             if (M('member')->where(array('user'=> $data['user']))->count()) {
                $this->error('用户名已被占用！');
            }
            $uid = M('member')->data($data)->add();
           $res=M('auth_group_access')->data(array('group_id' => $data['group_id'], 'uid' => $uid))->add();
            addlog('新增会员，会员UID：' . $uid);
        }else{
            $data['update_time']=time();
            M('auth_group_access')->data(array('group_id' => $data['group_id']))->where(array('uid' =>  $data['uid']))->save();
            addlog('编辑会员信息，会员UID：' . $uid);
            M('member')->data($data)->where(array('uid' =>  $data['uid']))->save();
        }
        $this->success('操作成功');

}
    public function add()
    {   
        $usergroup = M('auth_group')->field('id,title')->select();
        $this->assign('usergroup', $usergroup);
        $this->display('form');
    } 

    public  function getAreaInfo(){
        $pid =I('post.pid')?I('post.pid'):1;
        $city = M('city');
        $data = $city->where("pid=$pid")->select();
        echo $this->json($data);        
    }
    public function searchArea(){
        $data['prov'] = I('post.prov', '', 'strip_tags');
        $data['city'] = I('post.city', '', 'strip_tags');
        $data['dist'] = I('post.dist', '', 'strip_tags');
        $where = '';
        if(!empty($data['prov'])){
            $where="pid = 1";
        }
        if(!empty($data['city'])){
            $where.=" or pid={$data['prov']}";
        }
        if(!empty($data['dist'])){
            $where.= " or pid={$data['city']}";
        }
        if(empty($where)){
            echo $this->json();
        }else{
            $result=array();
            $city = M('city');
            $res = $city->where($where)->select();
            /* foreach ($res as $key=>$val){
                if($val['pid'] == 1){
                    $result['prov'][]=$val;
                }elseif($val['pid'] == $data['prov']){
                    $result['city'][]=$val;
                }elseif($val['pid'] == $data['city']){
                    $result['dist'][]=$val;
                }
            } */
            echo $this->json($res);
        }
        //var_dump($data);
        
    }
    //判断数组是否为空,并将结果转为json
    function json($data=array()){
        if(empty($data)){
            $res['status'] =0;
            $res['data']='no_data';
          }else{
            $res['status']=1;
            $res['data']=$data;
          }
          return json_encode($res);
    }
    
}
